//
//  EditInformationViewController.m
//  有材网
//
//  Created by 肖扬 on 16/1/20.
//  Copyright © 2016年 肖扬. All rights reserved.
//

#import "EditInformationViewController.h"
#import "EditHUDView.h"
#define NUMBERS @"0123456789+-"
#define NUMBEQQ @"0123456789"
@interface EditInformationViewController (){
    NSTimer *timer_;
}

@end

@implementation EditInformationViewController
@synthesize textField,viewInformation,viewUrl;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftBtn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    [self.rightBtn setTitle:@"完成" forState:0];
    [self.rightBtn setEnabled:NO];
//    [self.rightBtn setBackgroundColor:ButtonGrayColor];
    [self.rightBtn setTitleColor:ButtonGrayColor forState:0];
    [self.rightBtn addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
    UIView *bgView = [self createViewFrame:CGRectMake(0, 74, SWIDTH, 40) bgColor:[UIColor whiteColor] tag:0];
    [self.view addSubview:bgView];
    self.textField = [self createTextFieldFrame:CGRectMake(10, 74, SWIDTH-20, 40) bgColor:UIColorFromRGB(0x666666) tag:0 keyBoardType:0];
    self.textField.textAlignment = NSTextAlignmentLeft;
    self.textField.font = [UIFont systemFontOfSize:13];
    if ([self.title isEqualToString:@"修改QQ"]) {
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    if (![Unity isBlankString:self.viewInformation]) {
//        self.textField.placeholder = self.viewInformation;
        self.textField.text = self.viewInformation;
    }
    self.textField.delegate =self;
    self.textField.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.textField];
    
    [ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.textField];
    [self.textField becomeFirstResponder];
    // Do any additional setup after loading the view.
}
-(void)textChange
{
    self.rightBtn.enabled= self.textField.text.length>0;
    if (self.rightBtn.enabled == YES) {
//        [self.rightBtn setBackgroundColor:ClearColor];
        [self.rightBtn setTitleColor:WhiteColor forState:0];
    }else{
//        [self.rightBtn setBackgroundColor:ButtonGrayColor];
        [self.rightBtn setTitleColor:ButtonGrayColor forState:0];
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([self.title isEqualToString:@"修改公司电话"]) {
        NSCharacterSet*cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
        NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest) {
            return NO;
        }
        return YES;
    }else if ([self.title isEqualToString:@"修改QQ"]){
        NSCharacterSet*cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBEQQ] invertedSet];
        NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest) {
            return NO;
        }
        return YES;
    }
    return YES;
}
-(void)setMember:(NSMutableDictionary *)dic{
    TIME
    HEADDIC(nowTime, SETMEMBER);
    NSString *result = [Unity requestSomeApiData:HEAD withBody:dic];
    NSMutableDictionary *json = [result objectFromJSONString];
    if ([[json objectForKey:@"err_code"] integerValue]==0) {
        if ([self.title isEqualToString:@"修改姓名"]) {
            [[NSUserDefaults standardUserDefaults] setObject:self.textField.text forKey:@"nickname"];
        }
        [self.textField resignFirstResponder];
        EditHUDView *hudView = [[EditHUDView alloc]initWithFrame:CGRectMake(0, 64, SWIDTH, VHEIGHT)];
        hudView.backgroundColor = [UIColor clearColor];
        hudView.tag = 1010;
        hudView.alertLab.text = [json objectForKey:@"err_message"];
        [self.view addSubview:hudView];
        timer_ = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(doTime) userInfo:nil repeats:NO];
//        [self.navigationController popViewControllerAnimated:YES];
    }else{
        SHOW_ALERT([json objectForKey:@"err_message"]);
    }
    
}
-(void)setCompnay:(NSMutableDictionary *)dic{
    TIME
    HEADDIC(nowTime, SETCOMPANY);
    NSString *result = [Unity requestSomeApiData:HEAD withBody:dic];
    NSMutableDictionary *json = [result objectFromJSONString];
    if ([[json objectForKey:@"err_code"] integerValue]==0) {
        [self.textField resignFirstResponder];
        EditHUDView *hudView = [[EditHUDView alloc]initWithFrame:CGRectMake(0, 64, SWIDTH, VHEIGHT)];
        hudView.backgroundColor = [UIColor clearColor];
        hudView.tag = 1010;
        hudView.alertLab.text = [json objectForKey:@"err_message"];
        [self.view addSubview:hudView];
        timer_ = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(doTime) userInfo:nil repeats:NO];
//        [self.navigationController popViewControllerAnimated:YES];
    }else{
        SHOW_ALERT([json objectForKey:@"err_message"]);
    }
    
}
-(void)submitClick:(UIButton *)sender{
    if (self.textField.text.length ==0) {
        SHOW_ALERT(@"您还暂未输入信息");
        return;
    }
    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    if ([self.title isEqualToString:@"修改姓名"]) {
        [body setObject:self.textField.text forKey:@"nickname"];
        [self setMember:body];
    }else if ([self.title isEqualToString:@"修改QQ"]){
        [body setObject:self.textField.text forKey:@"qq"];
        [self setMember:body];
    }else if ([self.title isEqualToString:@"修改邮箱"]){
        if ([Unity verfierEmail:self.textField.text]) {
            NSLog(@"邮箱正确");
            [body setObject:self.textField.text forKey:@"email"];
            [self setMember:body];
        }else{
            EditHUDView *hudView = [[EditHUDView alloc]initWithFrame:CGRectMake(0, 64, SWIDTH, VHEIGHT)];
            hudView.backgroundColor = [UIColor clearColor];
            hudView.tag = 1010;
            hudView.alertLab.text = @"邮箱格式错误";
            [self.view addSubview:hudView];
            timer_ = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(doTime) userInfo:nil repeats:NO];
            NSLog(@"邮箱错误");
        }
    }else if ([self.title isEqualToString:@"修改公司名称"]){
        [body setObject:self.textField.text forKey:@"company_name"];
        [self setCompnay:body];
    }else if ([self.title isEqualToString:@"修改公司电话"]){
        [body setObject:self.textField.text forKey:@"tel"];
        [self setCompnay:body];
    }
}
-(void)doTime
{
    //alert过1秒自动消失
    [self resetTimer];
    [self removeHudView];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)removeHudView{
    EditHUDView *hudView = (EditHUDView *)[self.view viewWithTag:1010];
    if (hudView) {
        [hudView removeFromSuperview];
        hudView = nil;
    }
}
-(void) resetTimer
{
    if (timer_) {
        [timer_ invalidate];
        timer_ = nil;
    }
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 255) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
